package BookManage.dao.Impl;

import BookManage.BookManage;
import BookManage.dao.BaseDao;
import BookManage.dao.BookManageDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BookManageDaoImpl
        extends BaseDao implements BookManageDao {
    @Override
    public List<BookManage> list(int BookNo, int BookSize) {
        List<BookManage> list=new ArrayList<>();
        ResultSet rs=null;
        Object[] pampt={(BookNo-1)*BookSize,BookSize};
        try {
            String sql="select * from BookManage limit ?,?";
            rs=super.executeQuery(sql,pampt);
            while (rs.next()){
                BookManage bookManage=new BookManage();
                bookManage.setId(rs.getInt(1));
                bookManage.setName(rs.getString(2));
                bookManage.setAuthor(rs.getString(3));
                bookManage.setDate(rs.getString(4));
                bookManage.setType(rs.getString(5));
                list.add(bookManage);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    @Override
    public int getTotalCount() {
        ResultSet rs=null;
        int count=0;
        try {
            String sql="select count(1) from BookManage";
            rs=super.executeQuery(sql,null);
            if(rs.next()){
                count=rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeAll(rs,null,con);
        }
        return count;
    }

    @Override
    public int sc(String id) {
        int count=0;
        String sql="delete from BookManage where Id=?";
        Object[] pampt={id};
        count=super.executeUpdate(sql,pampt);
        if(count>0){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }
        return count;
    }

    @Override
    public int xz(BookManage bookManage) {
        int count=0;
        String sql="insert into BookManage(name,author,time,type)\n" +
                "values(?,?,?,?)";
        Object[] pampt={bookManage.getName(),bookManage.getAuthor(),bookManage.getDate(),bookManage.getType()};
        count=super.executeUpdate(sql,pampt);
        if(count>0){
            System.out.println("新增成功");
        }else{
            System.out.println("新增失败");
        }
        return count;
    }

    @Override
    public int xg(BookManage bookManage) {
        int count=0;
        String sql="update Bookmanage set name=?,author=?,time=?,type=? where Id=?";
        Object[] pmapt={bookManage.getName(),bookManage.getAuthor(),bookManage.getDate(),bookManage.getType(),bookManage.getId()};
        count=super.executeUpdate(sql,pmapt);
        if(count>0){
            System.out.println("修改成功");
        }else{
            System.out.println("修改失败");
        }
        return count;
    }
}
